package com.interview.score.service.impl;

import com.interview.score.common.Result;
import com.interview.score.common.ResultMessage;
import com.interview.score.common.StatusCode;
import com.interview.score.entity.ExaminerDrawLots;
import com.interview.score.entity.PsdStrategy;
import com.interview.score.entity.ScoreElement;
import com.interview.score.service.DataExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@Service
public class DataExchangeServiceImpl implements DataExchangeService {
    @Autowired
    private Environment environment;
    @Autowired
    private RestTemplate restTemplate;
    @Override
    public boolean checkGroupIdExists(String groupId) {
        String s = dataCenterConnection("group/checkGroupIdExist", "groupId", groupId);
        if ("1".equals(s)){
            return true;
        }else {
            return false;
        }
    }


    public boolean checkExamRoomCodeExist(String examRoomId) {
        String s = dataCenterConnection("examRoom/checkExamRoomCode", "examRoomId", examRoomId);
        if ("1".equals(s)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Map downLoadExameeDrawLotsSummary(String groupId) {
        String dataCenterURL = environment.getProperty("DataCenterURL");
        String targetURL = dataCenterURL+"/dataDistribute/exameeDrawLotsSummary";
        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("groupId",groupId);

        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        //发起请求
        Map result = restTemplate.postForObject(targetURL, request, Map.class);
        return result;
    }

    @Override
    public List<ExaminerDrawLots> downLoadExaminerDrawLotsInfoByExamRoomCode(String examRoomCode) {
        String dataCenterURL = environment.getProperty("DataCenterURL");
        String targetURL = dataCenterURL+"/dataDistribute/examinerDrawLotsDetail";

        ParameterizedTypeReference<List<ExaminerDrawLots>> responseType = new ParameterizedTypeReference<List<ExaminerDrawLots>>(){};
        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("examRoomCode",examRoomCode);

        // 组装请求体
        //发起请求(数据解析)
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<List<ExaminerDrawLots>> response = restTemplate.exchange(targetURL,
                HttpMethod.POST,
                request,
                responseType);
        List<ExaminerDrawLots> examinerDrawLotsList = response.getBody();
        return examinerDrawLotsList;
    }

    @Override
    public PsdStrategy getAvailablePasswordGenerationStrategy() {
        String dataCenterURL = environment.getProperty("DataCenterURL");
        String targetURL = dataCenterURL+"/dataDistribute/examinerAvailableLoginPasswordGenerationStrategy";

        ParameterizedTypeReference<PsdStrategy> responseType = new ParameterizedTypeReference<PsdStrategy>(){};

        ResponseEntity<PsdStrategy> response = restTemplate.exchange(targetURL,
                HttpMethod.POST,
                null,
                responseType);
        return response.getBody();
    }

    @Override
    public List<ScoreElement> downLoadScoreElementList() {
        String dataCenterURL = environment.getProperty("DataCenterURL");
        String targetURL = dataCenterURL+"/dataDistribute/scoreElementDetails";
        ParameterizedTypeReference<List<ScoreElement>> responseType = new ParameterizedTypeReference<List<ScoreElement>>(){};
        // 请求头设置,x-www-form-urlencoded格式的数据
        ResponseEntity<List<ScoreElement>> response = restTemplate.exchange(targetURL,
                HttpMethod.POST,
                null,
                responseType);
        List<ScoreElement> ScoreElementList = response.getBody();
        return ScoreElementList;

    }

    String dataCenterConnection(String targetURI,String key,String value){
        String dataCenterURL = environment.getProperty("DataCenterURL");
        String targetURL = dataCenterURL+targetURI;
        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add(key,value);

        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        //发起请求
        String result = restTemplate.postForObject(targetURL, request, String.class);
        return result;

    }
}
